package com.example.app.view.c2;

import android.content.Context;
import android.graphics.Rect;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.administrator.videoplay.R;
import com.example.app.activity.MLog;
import com.example.app.manager.c2.Camera2Manager;
import com.example.app.view.PreviewBaseLayout;

/**
 * Created by 郭敏 on 2018/3/21 0021.
 */
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public class PreviewC2Layout extends PreviewBaseLayout {

    public PreviewC2Layout(Context context) {
        super(context);

    }

    public PreviewC2Layout(Context context, AttributeSet attrs) {
        super(context, attrs);

    }

    public PreviewC2Layout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

    }


    private PreviewC2View pv;
    private View focusView;
    private int focusW = 250, focusH = 250;

    @Override
    protected void init(Context context) {
        pv = getSurfaceView(context);
        addView(pv);
        focusView = getFocusView(context);
        addView(focusView);
        focusView.setVisibility(View.INVISIBLE);
    }


    protected PreviewC2View getSurfaceView(Context context) {
        PreviewC2View previewView = new PreviewC2View(context);
        LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        previewView.setLayoutParams(lp);
        return previewView;
    }

    protected View getFocusView(Context context) {
        TextView tv = new TextView(context);
        LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        tv.setLayoutParams(lp);
        tv.setBackgroundResource(R.drawable.click_focus_bg);
        return tv;
    }

    private boolean isCalculate;

    private void setFocusViewWH() {
        if (focusView == null) {
            return;
        }
        if (isCalculate) {
            return;
        }
        isCalculate = true;
        ViewGroup.LayoutParams lp = focusView.getLayoutParams();
        lp.width = focusW;
        lp.height = focusH;
        focusView.setLayoutParams(lp);
    }

    public PreviewC2View getPreviewView() {
        return pv;
    }

    //0 没有 1：缩放事件 2：聚焦事件
    private int type;

    @Override
    protected void onTouch(MotionEvent event) {
        int action = event.getAction();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                type = 0;
                setFocusViewWH();
                onAnimationStop();
                focusView.setVisibility(View.INVISIBLE);
            default:
                //判断当前屏幕的手指数
                int pointerCount = event.getPointerCount();
                MLog.e("--------", "pointerCount:" + pointerCount);
                if (pointerCount > 1) {
                    //缩放事件
                    type = 2;
                    Camera2Manager.getInstance().setCameraZoom(event);
                    break;
                }
                if (action == MotionEvent.ACTION_UP && type == 0) {
                    //聚焦事件
                    type = 1;
                    int x = (int) event.getX();
                    int y = (int) event.getY();
                    LayoutParams lp = (LayoutParams) focusView.getLayoutParams();
                    lp.leftMargin = x - focusW / 4;
                    lp.topMargin = y - focusH / 4;
                    focusView.setLayoutParams(lp);
                    onFocusAnimator(focusView);
                }
                break;
        }
    }

    @Override
    protected void onAnimatorStart() {
        focusView.setVisibility(View.VISIBLE);
    }

    @Override
    protected void onAnimatorEnd() {
        int left = focusView.getLeft();
        int top = focusView.getTop();
        int right = focusView.getRight();
        int bottom = focusView.getBottom();
        MLog.e("--------", "left:" + left + " right:" + right + " top:" + top + " bottom:" + bottom);
        //
        int w = focusW / 4;
        int h = focusH / 4;
        Rect rect = new Rect(left + w, top + h,
                right - w, bottom - h);
        int rectW = rect.width();
        int rectH = rect.height();
        if (rectW < 0) {
            rect.right -= rectW;
        }
        if (rectH < 0) {
            rect.bottom -= rectH;
        }
        MLog.e("--------", rect.toString() +
                " w:" + rect.width() + " h:" + rect.height());
        Camera2Manager.getInstance().setCameraFocus(rect);
        focusView.setVisibility(View.INVISIBLE);
    }

}
